package cn.hahah.module.blog.controller.admin.userhistorybrowse;

import cn.hahah.module.blog.dal.dataobject.article.ArticleDO;
import cn.hahah.module.blog.dal.dataobject.users.UsersDO;
import cn.hahah.module.blog.service.article.ArticleService;
import cn.hahah.module.blog.service.users.UsersService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.userhistorybrowse.vo.*;
import cn.hahah.module.blog.dal.dataobject.userhistorybrowse.UserHistoryBrowseDO;
import cn.hahah.module.blog.service.userhistorybrowse.UserHistoryBrowseService;

@Tag(name = "管理后台 - 用户文章浏览记录")
@RestController
@RequestMapping("/blog/user-history-browse")
@Validated
public class UserHistoryBrowseController {

    @Resource
    private UserHistoryBrowseService userHistoryBrowseService;
    @Resource
    private UsersService usersService;
    @Resource
    private ArticleService articleService;

    @PostMapping("/create")
    @Operation(summary = "创建用户文章浏览记录")
    @PreAuthorize("@ss.hasPermission('blog:user-history-browse:create')")
    public CommonResult<Long> createUserHistoryBrowse(@Valid @RequestBody UserHistoryBrowseSaveReqVO createReqVO) {
        return success(userHistoryBrowseService.createUserHistoryBrowse(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户文章浏览记录")
    @PreAuthorize("@ss.hasPermission('blog:user-history-browse:update')")
    public CommonResult<Boolean> updateUserHistoryBrowse(@Valid @RequestBody UserHistoryBrowseSaveReqVO updateReqVO) {
        userHistoryBrowseService.updateUserHistoryBrowse(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户文章浏览记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:user-history-browse:delete')")
    public CommonResult<Boolean> deleteUserHistoryBrowse(@RequestParam("id") Long id) {
        userHistoryBrowseService.deleteUserHistoryBrowse(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户文章浏览记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:user-history-browse:query')")
    public CommonResult<UserHistoryBrowseRespVO> getUserHistoryBrowse(@RequestParam("id") Long id) {
        UserHistoryBrowseDO userHistoryBrowse = userHistoryBrowseService.getUserHistoryBrowse(id);
        return success(BeanUtils.toBean(userHistoryBrowse, UserHistoryBrowseRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户文章浏览记录分页")
    @PreAuthorize("@ss.hasPermission('blog:user-history-browse:query')")
    public CommonResult<PageResult<UserHistoryBrowseRespVO>> getUserHistoryBrowsePage(@Valid UserHistoryBrowsePageReqVO pageReqVO) {
        PageResult<UserHistoryBrowseDO> pageResult = userHistoryBrowseService.getUserHistoryBrowsePage(pageReqVO);
        PageResult<UserHistoryBrowseRespVO> result = BeanUtils.toBean(pageResult, UserHistoryBrowseRespVO.class);
        for (UserHistoryBrowseRespVO userHistoryBrowseRespVO : result.getList()) {
            UsersDO usersDO = usersService.getUsers(userHistoryBrowseRespVO.getUserId());
            ArticleDO articleDO = articleService.getArticle(userHistoryBrowseRespVO.getArticleId());

            if( usersDO != null ){
                userHistoryBrowseRespVO.setUserName(usersDO.getUsername());
            }
            if( articleDO != null ){
                userHistoryBrowseRespVO.setArticleName(articleDO.getTitle());
            }
        }

        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户文章浏览记录 Excel")
    @PreAuthorize("@ss.hasPermission('blog:user-history-browse:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserHistoryBrowseExcel(@Valid UserHistoryBrowsePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserHistoryBrowseDO> list = userHistoryBrowseService.getUserHistoryBrowsePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户文章浏览记录.xls", "数据", UserHistoryBrowseRespVO.class,
                        BeanUtils.toBean(list, UserHistoryBrowseRespVO.class));
    }

}